<?php

namespace Alipay\Common;

use Alipay\Http\Response;

class Alipay
{
    protected $appId;

    protected $privateKey;

    protected $privateKeyFile;

    protected $alipayPublicKey;

    protected $gateway = 'https://openapi.alipay.com/gateway.do';

    protected $returnUrl = '';

    protected $timeout = 5;

    protected $format = 'JSON';

    protected $charset = 'utf-8';

    protected $version = '1.0';

    protected $signType = 'RSA2';

    protected $httpDebug = false;

    protected $redirectUrl = '';

    //支付宝根证书
    protected $alipayRootCert;

    //支付宝公钥证书
    protected $alipayPublicCert;

    //应用公钥证书
    protected $appPublicCert;

    protected $isCertModel = false;

    /**
     * @var $certClient CertClient
     */
    protected $certClient;

    protected $logsDir;

    protected $proxy;

    /**
     * @var \Alipay\Http\Request
     */
    private $request;

    /**
     * @return mixed
     */
    public function getPrivateKey()
    {
        return $this->privateKey;
    }

    /**
     * @return mixed
     */
    public function getPrivateKeyFile()
    {
        return $this->privateKeyFile;
    }

    /**
     * @return mixed
     */
    public function getAlipayPublicKey()
    {
        return $this->alipayPublicKey;
    }

    /**
     * @param int $second
     * @return $this
     * @author xis
     */
    public function setTimeout(int $second): self
    {
        $this->timeout = $second;
        return $this;
    }

    /**
     * @param string $format
     * @return self
     */
    public function setFormat(string $format): self
    {
        $this->format = $format;
        return $this;
    }

    /**
     * @param string $charset
     * @return self
     */
    public function setCharset(string $charset): self
    {
        $this->charset = $charset;
        return $this;
    }

    /**
     * @param string $version
     * @return self
     */
    public function setVersion(string $version): self
    {
        $this->version = $version;
        return $this;
    }

    /**
     * @param string $signType
     * @return self
     */
    public function setSignType(string $signType): self
    {
        $this->signType = $signType;
        return $this;
    }

    /**
     * @param bool $httpDebug
     * @return self
     */
    public function setHttpDebug(bool $httpDebug): self
    {
        $this->httpDebug = $httpDebug;
        return $this;
    }

    /**
     * @param mixed $proxy
     */
    public function setProxy($proxy): self
    {
        $this->proxy = $proxy;
        return $this;
    }

    /**
     * @param mixed $gateway
     */
    public function setGateway($gateway): self
    {
        $this->gateway = $gateway;
        return $this;
    }

    public function request($method, $param, string $response, bool $returnUrl = false): Response
    {
        if (!$this->request instanceof \Alipay\Http\Request) {
            $this->request = new \Alipay\Http\Request();
            $this->request->setTimeout($this->timeout);
            $this->request->setDebug($this->httpDebug);
            $this->request->setLogsDir($this->logsDir);
        }
        $client = $this->request;

        $client->setUrl($this->gateway);

        $client->setMethod($method);

        if ($param) {
            $client->setGetParam($param);
        }
        if ($method == 'post') {
            $client->setFormData($param);
        }

        return $client->request($response, $returnUrl);
    }

}
